package api

import (
	"community_end/middleware"
	"community_end/model"
	"community_end/utils/errmsg"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"strings"
)

func GetPostList(ctx *gin.Context) {
	page := ctx.Query("pageNo")
	pageSize := ctx.Query("size")
	pageInt, _ := strconv.Atoi(page)
	pageSizeInt, _ := strconv.Atoi(pageSize)

	//fmt.Println(page)
	//fmt.Println(pageSize)

	_, count := model.SelectAllPost()

	posts, code := model.SelectPostList(pageInt, pageSizeInt)
	ctx.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"data":    posts,
		"current": page,
		"total":   count,
		"size":    pageSize,
	})
	return
}

func InsertPost(ctx *gin.Context) {
	m := make(map[string]interface{})
	ctx.ShouldBindJSON(&m)
	tags := m["tags"].([]interface{})

	// 查询用户ID
	token := ctx.Request.Header.Get("Authorization")
	userToken, _ := middleware.AuthToken(strings.Split(token, " ")[1])
	user, _ := model.SelectUser(userToken.UserName)

	// 组装帖子信息，获取标签，如果有就查询，没有就创建，因为没有后台标签管理，这里有点复杂
	var post model.Post
	post.Title = m["title"].(string)
	post.Content = m["content"].(string)
	post.UserID = int(user.ID)
	var tmepTags []model.Tag
	for _, v := range tags {
		t := v.(string)
		tempt, c := model.SelectTagByName(t)
		if c != errmsg.SUCCESS {
			var temp model.Tag
			temp.Name = t
			temp.TopicCount++
			tag, _ := model.InsertTag(temp)
			tempt = tag
		}
		tmepTags = append(tmepTags, tempt)
	}
	post.Tags = append(post.Tags, tmepTags...)
	id, code := model.InsertPost(post)

	u, _ := model.SelectUserByID(int(user.ID))
	u.TopicCount++
	model.SaveUser(u)

	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  errmsg.GetErrMsg(code),
		"id":   id,
	})
	return
}

func GetPostDetail(ctx *gin.Context) {
	postID := ctx.Query("id")
	//fmt.Println(postID)

	// 获取话题数据
	post, _ := model.SelectPostByID(postID)

	// 获取用户数据
	user, _ := model.SelectUserByID(post.UserID)

	ctx.JSON(http.StatusOK, gin.H{
		"code": errmsg.SUCCESS,
		"msg":  errmsg.GetErrMsg(errmsg.SUCCESS),
		"data": post,
		"user": user,
	})
}

func GetRecommendTopic(ctx *gin.Context) {
	topicID := ctx.Query("id")
	fmt.Println(topicID)

	topics, _ := model.GetRecommendTopic()
	ctx.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  errmsg.GetErrMsg(200),
		"data": topics,
	})
	return
}

func UpdatePost(ctx *gin.Context) {

	post := model.Post{}
	ctx.ShouldBindJSON(&post)

	code := model.SavePost(post)
	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  errmsg.GetErrMsg(code),
		"id":   post.ID,
	})
	return
}

func DeleteTopic(ctx *gin.Context) {
	topicID := ctx.Param("id")
	fmt.Println(topicID)

	code := model.DeleteTopic(topicID)

	ctx.JSON(http.StatusOK, gin.H{
		"code": code,
		"msg":  errmsg.GetErrMsg(code),
	})
}

func SelectLikeKeyword(ctx *gin.Context) {
	key := ctx.Query("keyword")
	pageNum := ctx.Query("pageNum")
	pageSize := ctx.Query("pageSize")
	posts, code := model.SelectLikeKeyword(key)

	fmt.Println(key)
	fmt.Println(pageNum)
	fmt.Println(pageSize)

	ctx.JSON(http.StatusOK, gin.H{
		"code":    code,
		"msg":     errmsg.GetErrMsg(code),
		"total":   len(posts),
		"size":    pageSize,
		"current": pageNum,
		"data":    posts,
	})
	return
}
